package fun.qu_an.minecraft.fabric.fzbbl.mixin;

import fun.qu_an.minecraft.fabric.fzbbl.event.PlayerPlacedCallback;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(BlockItem.class)
public class PlayerPlacedMixin {
	@Redirect(method = "place(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/util/ActionResult;", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;onPlaced(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/entity/LivingEntity;Lnet/minecraft/item/ItemStack;)V"))
	private void onPlayerUsed(Block instance, World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
		instance.onPlaced(world, pos, state, placer, stack);
		if (!(placer instanceof PlayerEntity player)) return;
		PlayerPlacedCallback.EVENT.invoker().interact(player, world, pos, stack);
	}
}
